# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from okuz.profile.models import Profile
from okuz.profile.forms import ProfileForm


def show (request, id):
	try:
		profile = Profile.objects.get (id = id);
	except:
		return error (request, u"Wystąpił błąd ;-(", u"Nie mogę pobrać profilu dla id = "+id);
	return render_to_response ('profile/show.html', {'profile':profile},
								context_instance = RequestContext (request));


def add (request):
	if request.POST:
		form = ProfileForm (request.POST);
		if form.is_valid ():
			profile = form.save ();
			return redirect ('/profile/%s' % profile.id);
	else:
		form = ProfileForm ();
	return render_to_response ('profile/add.html', {'form':form},
								context_instance = RequestContext (request));


def edit (request, id):
	if request.POST:
		form = ProfileForm (data = request.POST or None, instance = Profile.objects.get (id = id));
		if form.is_valid ():
			form.save ();
		return redirect ('/profile/' + id);
	else:
		form = ProfileForm (instance = Profile.objects.get (id = id));
	return render_to_response ('profile/edit.html', {'form':form},
								context_instance = RequestContext (request));


def remove (request, id):
	try:
		profile = Profile.objects.get (id = id);
	except:
		return error (request, u"Wystąpił błąd ;-(", u"Nie mogę pobrać profilu dla id = "+id);
	return render_to_response ('profile/remove.html', {'profile':profile},
								context_instance = RequestContext (request));


def delete (request, id):
	try:
		profile = Profile.objects.get (id = id);
		profile.delete ();
	except:
		return error (request, u"Wystąpił błąd ;-(", u"Nie mogę pobrać profilu dla id = "+id);
	return redirect ('/');


def error (request, title, message):
	return render_to_response ('info.html', {'title':title, 'msg':message},
								context_instance = RequestContext (request));

